//
//  settingResetPwViewController.m
//  SoundPill
//
//  Created by 宋旭东 on 15/9/6.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "settingResetPwViewController.h"
#import "CustomRegister.h"
#import "AFNetworking.h"
#import "JCAlertView.h"
@interface settingResetPwViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) CustomRegister *password;
@property (strong, nonatomic) CustomRegister *rePassword;
@end

@implementation settingResetPwViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setFrame:CGRectMake(0, 0, 30, 30)];
    [back setImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backToset) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;

    UIBarButtonItem *publishItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"done"] style:UIBarButtonItemStylePlain target:self action:@selector(doneWithPassword)];
    publishItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = publishItem;
    // Do any additional setup after loading the view.
    self.password = [[CustomRegister alloc] initWithFrame:CGRectMake(25, 25, self.view.frame.size.width - 50, 35) image:[UIImage imageNamed:@"password@3x"] placeHold:@"输入新密码"];
    self.password.field.secureTextEntry = YES;
    self.password.field.returnKeyType = UIReturnKeyDone;
    _password.field.delegate = self;
    [self.view addSubview:self.password];
    self.rePassword = [[CustomRegister alloc] initWithFrame:CGRectMake(25, _password.frame.size.height + _password.frame.origin.y + 5, self.view.frame.size.width - 50, 35) image:[UIImage imageNamed:@"ensurePassword"] placeHold:@"确认新密码"];
    self.rePassword.field.secureTextEntry = YES;
    self.rePassword.field.returnKeyType = UIReturnKeyDone;
    _rePassword.field.delegate = self;
    [self.view addSubview:_rePassword];
    [_password.field becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _rePassword.field) {
        if ([_rePassword.field.text isEqualToString:_password.field.text]) {
            [self postNewPassword:_password.field.text rePassword:_rePassword.field.text];
        } else {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"两次输入的密码不相同，请重新输入" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//            [alertView show];
//            [self performSelector:@selector(dismissAlert:) withObject:alertView afterDelay:1];
            UILabel *customView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 40)];
            customView.backgroundColor = [UIColor whiteColor];
            customView.text = @"两次输入的密码不相同，请重新输入";
            customView.textAlignment = NSTextAlignmentCenter;
            customView.layer.borderColor = [[UIColor blackColor] CGColor];
            customView.layer.borderWidth = 2;
            JCAlertView *_alert = [[JCAlertView alloc] initWithCustomView:customView dismissWhenTouchedBackground:NO];
            [_alert show];
            [_alert performSelector:@selector(dismissWithCompletion:) withObject:nil afterDelay:1.5];
            _rePassword.field.text = nil;
        }
    }else {
        if (_password.field.text.length >= 6) {
            [_rePassword.field becomeFirstResponder];
        } else {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"密码位数不正确,至少6位" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//            [alertView show];
//            [self performSelector:@selector(dismissAlert:) withObject:alertView afterDelay:1];
            UILabel *customView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 40)];
            customView.backgroundColor = [UIColor whiteColor];
            customView.text = @"密码位数不争取,至少6位";
            customView.textAlignment = NSTextAlignmentCenter;
            customView.layer.borderColor = [[UIColor blackColor] CGColor];
            customView.layer.borderWidth = 2;
            JCAlertView *_alert = [[JCAlertView alloc] initWithCustomView:customView dismissWhenTouchedBackground:NO];
            [_alert show];
            [_alert performSelector:@selector(dismissWithCompletion:) withObject:nil afterDelay:1.5];
        }
    }
    return YES;
}
- (void)postNewPassword:(NSString *)password rePassword:(NSString *)rePassword
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:password,@"password",rePassword,@"repassword", nil];
    NSString *url = @"http://123.57.1.143/api/user/change";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)dismissAlert:(UIAlertView *)alertView
{
    if (alertView) {
        [alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:YES];
    }
}
- (void)backToset
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)doneWithPassword
{
    if (_password.field.text != 0 && [_password.field.text isEqualToString:_rePassword.field.text]) {
        [self postNewPassword:_password.field.text rePassword:_rePassword.field.text];
    } else {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请确保两次密码输入相同" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert show];
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"请确保两次密码输入相同" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
